通过 Tipdm 的数据处理获得的数据预处理的一些个方法及流程。

对时间序列的处理

1
2
3
4
5
6
7
8
# 使用 pands 的 to_datetime 方法,可以将字符类型的时间字段转换为时间字段
vehicle['location_time'] = pd.to_datetime(vehicle['location_time'])

# 将时间字段补点,按照 1s 的频度
grouper = pd.Grouper(key='location_time', freq='1s')

# 使用 reset_index() 重新建立 dataframe 的索引
res = vehicle.groupby(grouper).first().reset_index()

NAN值的处理

1
2
3
4
5
6
7
8
# 将 dataframe中的 "lng","lat","gps_speed" 三列转换为 二维的 numpy 数组
lng_lat = res[["lng","lat","gps_speed"]].values

# 通过 np.isnan 函数判断该数组中为值为 NAN 的项,并返回一个 True/False 形成的数组
isnull_state = np.isnan(lng_lat)

# 通过 np.where 函数找到 isnull_state 中为True的索引(行号和列号),通常和 np.isnan 联合使用
nan_index = np.array(sorted(list(set(np.where(isnull_state)[0]))))

pandas中行列索引

1
2
# 对于一个DataFrame A,A.loc[k]是读取A中index为k的那一行。A.iloc[k]是读取A中的第k行。
lng = np.array([test_vehicle.loc[i]["lng"] for i in range(2500,2700)])

numpy数组的初始化及删除

1
2
3
4
5
6
7
# 初始化一个二维的数组,并赋值为0,现在未找到可以直接创建一个空的二维数组的方法
all_point_distance = np.array([[0,0,0]])
# 再通过 np.delete 函数删除第一行。多说一句,axis=0 是对行操作,axis=1 是对列进行操作
all_point_distance = np.delete(all_point_distance,0,axis=0)

# 之所以这样做,是因为有数组合并的需求。如下,这样就可以把同样是二维数组的 distance 中的元素添加到 all_point_distance
all_point_distance = np.concatenate((all_point_distance,distance))